#pragma once
#include<iostream>
#include<vector>
namespace mutou{
template<size_t N>
class bitset
{
    public:
        bitset()
        {
         Bitset.resize(N/32+1);   
        }
        bool test(size_t n)
        //查看这位是1还是0
        {
            size_t i = n/32;
            size_t j = n%32;
            return Bitset[i]&(1<<j);
        }
        void set(size_t n)
        //把这位设为1
        {
            int i = n/32;
            int j = n%32;
            Bitset[i]|=(1<<j);
        }
        void reset(size_t n)
        //把这位设为0
        {
            int i = n/32;
            int j = n%32;
            Bitset[i]&=(~(1<<j));
        }

    private:
    std::vector<size_t> Bitset;

};
    template<size_t N>
    class twobitset
    {   
        public:
        void set(size_t n)
        {
            bool bs1 = _bs1.test(n);
            bool bs2 = _bs2.test(n);
            if(!bs1&&!bs2)
            {
                //表示二者全都没有 00->01
                _bs1.set(n);
            }
            else if(bs1&&!bs2)
            {
                //表示出现了1次要出现第二次 01->10
                _bs1.reset(n);
                _bs2.set(n);
            }
            else{
                //出现2次以上了,要出现第三次
                //10->11
                _bs1.set(n);
            }

    }
        int getcount(size_t n)
        {
            bool bs1 = _bs1.test(n);
            bool bs2 = _bs2.test(n);
            if(!bs1&&!bs2)
            {
                //表示二者全都没有 00->01
                return 0;
            }
            else if(bs1&&!bs2)
            {
                //表示出现了1次要出现第二次 01->10
               return 1;
            }
            else if(!bs1&&bs2){
                //出现2次以上了,要出现第三次
                //10->11
                return 2;
            }
            else{
                return 3;
            }
        }

        private:
        bitset<N> _bs1;
        bitset<N> _bs2;

    };
    void test()
    {
        int a[] = { 5,7,9,2,5,99,5,5,7,5,3,9,2,55,1,5,6,6,6,6,7,9 };
        mutou::twobitset<100> tws;
        for(auto &e:a)
        {
            tws.set(e);
        }
        for(int i = 0;i<100;i++)
        {
            if(tws.getcount(i)==1||tws.getcount(i)==2)
            {
               std::cout<<i<<std::endl;
            }
        }
    }
    void test_bitset1()
{
	int a1[] = { 5,7,9,2,5,99,5,5,7,5,3,9,2,55,1,5,6 };
	int a2[] = { 5,3,5,99,6,99,33,66 };

	bitset<100> bs1;
	bitset<100> bs2;

	for (auto e : a1)
	{
		bs1.set(e);
	}

	for (auto e : a2)
	{
		bs2.set(e);
	}

	for (size_t i = 0; i < 100; i++)
	{
		if (bs1.test(i) && bs2.test(i))
		{
			std::cout << i <<std::endl;
		}
	}
}
}